package com.repair.system.controller;

import com.repair.system.dto.CoordinateData;
import com.repair.system.service.FileUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

@Controller
@RequestMapping("/upload")
public class FileUploadController {

    @Autowired
    private FileUploadService fileUploadService;

    @Value("${upload.path:uploads}")
    private String uploadPath;

    @GetMapping
    public String uploadPage() {
        return "upload/upload";
    }

    @PostMapping("/coordinates")
    @ResponseBody
    public ResponseEntity<?> uploadCoordinateFile(@RequestParam("file") MultipartFile file) {
        try {
            if (!fileUploadService.isValidCoordinateFile(file.getOriginalFilename())) {
                return ResponseEntity.badRequest()
                    .body(Map.of("error", "不支持的文件格式，请上传 .csv, .xy 或 .txt 文件"));
            }

            List<CoordinateData> coordinates = fileUploadService.parseCoordinateFile(file);
            String savedFilename = fileUploadService.saveUploadedFile(file, uploadPath + "/coordinates");
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("filename", savedFilename);
            response.put("coordinates", coordinates);
            response.put("count", coordinates.size());
            
            return ResponseEntity.ok(response);
            
        } catch (Exception e) {
            return ResponseEntity.badRequest()
                .body(Map.of("error", "文件处理失败: " + e.getMessage()));
        }
    }

    @PostMapping("/image")
    @ResponseBody
    public ResponseEntity<?> uploadImageFile(@RequestParam("file") MultipartFile file) {
        try {
            if (!fileUploadService.isValidImageFile(file.getOriginalFilename())) {
                return ResponseEntity.badRequest()
                    .body(Map.of("error", "不支持的图片格式，请上传 .jpg, .jpeg 或 .png 文件"));
            }

            String savedFilename = fileUploadService.saveUploadedFile(file, uploadPath + "/images");
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("filename", savedFilename);
            response.put("url", "/uploads/images/" + savedFilename);
            
            return ResponseEntity.ok(response);
            
        } catch (Exception e) {
            return ResponseEntity.badRequest()
                .body(Map.of("error", "图片上传失败: " + e.getMessage()));
        }
    }

    @PostMapping("/pdf")
    @ResponseBody
    public ResponseEntity<?> uploadPdfFile(@RequestParam("file") MultipartFile file) {
        try {
            if (!fileUploadService.isValidPdfFile(file.getOriginalFilename())) {
                return ResponseEntity.badRequest()
                    .body(Map.of("error", "请上传 .pdf 格式的文件"));
            }

            String savedFilename = fileUploadService.saveUploadedFile(file, uploadPath + "/pdfs");
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("filename", savedFilename);
            response.put("url", "/uploads/pdfs/" + savedFilename);
            
            return ResponseEntity.ok(response);
            
        } catch (Exception e) {
            return ResponseEntity.badRequest()
                .body(Map.of("error", "PDF上传失败: " + e.getMessage()));
        }
    }

    @GetMapping("/coordinates/{filename}")
    @ResponseBody
    public ResponseEntity<?> getCoordinates(@PathVariable String filename) {
        try {
            // 这里可以实现从存储中重新读取坐标数据
            return ResponseEntity.ok(Collections.emptyList());
        } catch (Exception e) {
            return ResponseEntity.badRequest()
                .body(Map.of("error", "读取坐标文件失败"));
        }
    }
}
